#############################
# author:mckelvin
# date:02/04/2012
# script to valid host 
#############################

import httplib
from urlparse import urlparse

HOSTS_FILE = '''.\hosts'''
FOECE_HTTPS = ['facebook.com','twitter.com', 'appspot.com' ]
TIME_OUT=300
def check_host_via_http(addr, url, method="GET"):
	o = urlparse(url)
	scheme = o[0]
	netloc = o[1]
	path = o[2]
	params = o[3]
	headers = {
		"Host":(netloc)
		,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"
		,"Content-Type":"text/html; charset=utf-8"
	}
	try:
		if scheme != "https":
			conn = httplib.HTTPConnection(addr, timeout=TIME_OUT)
		else:
			conn = httplib.HTTPSConnection(addr, timeout=TIME_OUT)
		conn.request(method, path, params, headers)
		r1 = conn.getresponse()
		data = r1.read()
		r1.close()
		conn.close()
		print r1.status, 
		if r1.status != 404 :
			#print data
			return True
		else:
			return False
	except:
		print "\t#BLOCKED!", 
		return False
	finally:
		conn.close()


def check_host(ip, host):
	if check_host_via_http(ip, host)  ==  True:
		print  ip + "\t" + host + "\t# :)"
	else:
		print   ip + "\t" + host + "\t# :("

def test():
	'''test'''
	check_host("69.63.189.16", "https://facebook.com")
	check_host("107.20.215.211", "blog.mckelv.in")
	check_host("107.20.215.211", "www.mckelv.in")
	check_host("199.59.149.243", "https://twitter.com")

def main():
	reader = open(HOSTS_FILE, 'r')
	for line in reader:
		if(line[0] == "#"):
			continue
		try:
			ip, host = line.split()
			for eachfucker in FOECE_HTTPS:
				if(host.endswith(eachfucker)):
					host = "https://" + host
					host = "http://" + host
			check_host(ip, host); 
		except:
			continue

main()
